#drafted by Jeremy VanDerWal ( jjvanderwal@gmail.com ... www.jjvanderwal.com )
#GNU General Public License .. feel free to use / distribute ... no warranties

################################################################################
#run this on uq using
#qsub -l select=1:ncpus=4:NodeType=medium -l walltime=100:00:00 -A q1086 -I

################################################################################
#define the variables that may be changed...
base.data.dir = '/home/jc148322/Climate/AWAP.direct.download/' #the base output directory

################################################################################
###set up variables to cycle through given 3 different variables... rain, tmin, tmax
vars = c('rain.m','tmin.m','tmax.m','solar.m') #define the variable order
out.dirs = paste(base.data.dir, c('rain/monthly/','tmin/monthly/','tmax/monthly/','solar/monthly/'),sep='') #define the output directories
url.strs = c('http://www.bom.gov.au/web03/ncc/www/awap/rainfall/totals/month/grid/0.05/history/nat/',
	'http://www.bom.gov.au/web03/ncc/www/awap/temperature/minave/month/grid/0.05/history/nat/',
	'http://www.bom.gov.au/web03/ncc/www/awap/temperature/maxave/month/grid/0.05/history/nat/',
	'http://www.bom.gov.au/web03/ncc/www/awap/solar/solarave/month/grid/0.05/history/nat/')#define the url strings
sh.dir = '/home/jc148322/Climate/AWAP.direct.download/pbs.scripts/'; setwd(sh.dir) #define the sh directory

###define all possible dates
start.year = '2011'; end.year = as.numeric(format(Sys.Date(),'%Y'))#define start and end dates
dates=expand.grid(years=start.year:end.year,month=1:12)
start.dates = as.Date(paste(dates$years,'-',sprintf('%02i',dates$month),'-','01',sep=''))#work out the start and end dates
end.dates = start.dates; for (jj in 1:length(start.dates)) { end.dates[jj] = seq(start.dates[jj],length=2,by='+1 month')[2] - 1 } #get the last day of the month
start.dates = format(start.dates,'%Y%m%d'); end.dates = format(end.dates,'%Y%m%d')


#cycle through rain, tmin, tmax
for (ii in 1:4) {
#start a sh file to submit
	zz = file(paste(sh.dir,vars[ii],'.sh',sep=''),'w')
		cat('cd ',out.dirs[ii],'\n',sep='',file=zz) #move to the directory
		#cycle through the dates
		for (jj in 1:length(start.dates)){
			tstr = paste(start.dates[jj],end.dates[jj],sep='') #defien the date part of the string
			full.str = paste(url.strs[ii],tstr,'.grid.Z',sep='') #define the full url
			#write info out to file
			if(!file.exists(paste(out.dirs[ii],substr(tstr,1,6),'.asc.gz',sep=''))) { #if file does not already exist download it
				cat('\n',file=zz)
				cat('wget ',full.str,'\n',sep='',file=zz) #download the file
				cat('gzip -df ',tstr,'.grid.Z\n',sep='',file=zz) #uncompress it
				cat('mv ',tstr,'.grid',' ',substr(tstr,1,6),'.asc\n',sep='',file=zz) #rename the file
				cat('gzip ',substr(tstr,1,6),'.asc\n',sep='',file=zz) #recompress to gz file
				cat('\n',file=zz)
			}
		}
		cat('rm -f *grid.Z\n',file=zz) #remove any .grid.Z files remaining
	close(zz)#close the file
}

#go to the sh dir and submit the jobs
for (ii in 1:4) system(paste('qsub -m n ',vars[ii],'.sh',sep=''))
